import behaviors from "../../../../../config/behaviors";
import UserModel from "../../../../../models/User";
import ProductModel from "../../../../../models/Product";
import Crypt from "../../../../../utils/Crypt";
import CartModel from "../../../../../models/Cart";
import CartTotal from "../../../../../utils/CartTotal";

const userModel = new UserModel()
const productModel = new ProductModel()
const cartModel = new CartModel()

Component({
  behaviors: [behaviors],
  properties: {
    product: {
      type: Object,
      value: {},
      observer(newVal, oldVal, changedPath) {
        if (newVal && newVal.id) {
          this.checkFav()
          this.checkCart()
        }
      }
    },
    image: Object,
    group: String,
    haveSku: false,
    cart_count_text: ''
  },
  data: {
    isFav: false
  },
  methods: {
    onHome() {
      wx.switchTab({
        url: '/pages/home/index'
        // url: '/pages/index/index'
      })
    },

    onCart() {
      wx.switchTab({
        url: '/pages/cart/index'
      })
    },

    onService() {
      wx.navigateTo({
        url: '/sub_common/pages/service/index'
      })
    },

    checkCart() {
      userModel.checkLogin(false).then(() => {
        cartModel.list().then(response => {
          let { data } = response
          data = Crypt.parse(data)
          const count = CartTotal.getTotal(data)
          this.setData({
            cart_count_text: count
          })
        })
      })
    },

    checkFav() {
      userModel.checkLogin(false).then(() => {
        const id = this.properties.product.id
        productModel.checkFav({ id }).then(response => {
          let { data } = response
          data = Crypt.parse(data)
          this.setData({
            isFav: !!data.is
          })
          // console.log(data)
        })
      })

    },

    onFav() {
      if (!this.properties.product) {
        return false
      }
      const id = this.properties.product.id
      userModel.checkLogin().then(() => {
        if (!this.data.isFav) {
          productModel.createFav({ id }).then(() => {
            this.setData({
              isFav: true
            })
            wx.showToast({
              title: '收藏成功'
            })
          })
        } else {
          productModel.removeFav({ id }).then(() => {
            this.setData({
              isFav: false
            })
            wx.showToast({
              title: '取消成功'
            })
          })
        }
      })
    },

    addCart(e) {
      userModel.checkLogin().then(() => {
        if (this.properties.haveSku) {
          this.triggerEvent('openPop', { type: 1 })
        } else {
          const stock = +this.properties.product.stock
          if (stock < 1) {
            return wx.showToast({ title: '库存不存', icon: 'error' })
          } else {
            this.triggerEvent('addCart')
          }
        }
      })
    },

    buy(e) {
      userModel.checkLogin().then(() => {
        if (this.properties.haveSku) {
          this.triggerEvent('openPop', { type: 2 })
        } else {
          const stock = +this.properties.product.stock
          if (stock < 1) {
            return wx.showToast({ title: '库存不存', icon: 'error' })
          } else {
            this.triggerEvent('buy')
          }
        }
      })
    },

    createGroup(e) {
      userModel.checkLogin().then(() => {
        if (this.properties.haveSku) {
          this.triggerEvent('openPop', { type: 3 })
        } else {
          const stock = +this.properties.product.stock
          if (stock < 1) {
            return wx.showToast({ title: '库存不存', icon: 'error' })
          } else {
            this.triggerEvent('createGroup')
          }
        }
      })
    },

    randomGroup(e) {
      userModel.checkLogin().then(() => {
        if (this.properties.haveSku) {
          this.triggerEvent('openPop', { type: 5 })
        } else {
          const stock = +this.properties.product.stock
          if (stock < 1) {
            return wx.showToast({ title: '库存不存', icon: 'error' })
          } else {
            this.triggerEvent('randomGroup')
          }
        }
      })
    },

    joinGroup(e) {
      userModel.checkLogin().then(() => {
        if (this.properties.haveSku) {
          this.triggerEvent('openPop', { type: 4 })
        } else {
          const stock = +this.properties.product.stock
          if (stock < 1) {
            return wx.showToast({ title: '库存不存', icon: 'error' })
          } else {
            this.triggerEvent('joinGroup')
          }
        }
      })
    }

  },
  lifetimes: {
    ready() {
      // console.log(this.properties.haveSku)
    }
  },

});
